home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro18 / keyboard.qb4 < prev    next >
Text File  |  1991-10-14  |  3KB  |  104 lines

  1. 'KEYBOARD.BAS - From the GWBASIC Tutorial Series, #11
  2. 'October, 1991
  3.  
  4. 'To illustrate key trapping, let's use a simple little program that just
  5. 'prints a message on the screen, changing the color every once in a while,
  6. 'then when one of the 'trapped' keys is pressed beeps and prints a message
  7. 'at the bottom of the screen.
  8.  
  9. 'First, we define the 'user' keys:
  10.  
  11.     KEY 16, CHR$(&H8) + CHR$(&H1E)
  12.     KEY 17, CHR$(&H8) + CHR$(&H20)
  13.     KEY 18, CHR$(&H8) + CHR$(&H21)
  14.     KEY 19, CHR$(&H8) + CHR$(&H19)
  15.     KEY 20, CHR$(&H0) + CHR$(&H1)
  16.  
  17. 'Next, we need to turn on key trapping...
  18.  
  19.     KEY(1) ON
  20.     KEY(13) ON
  21.     KEY(12) ON
  22.     KEY(16) ON
  23.     KEY(17) ON
  24.     KEY(18) ON
  25.     KEY(19) ON
  26.     KEY(20) ON
  27.  
  28. 'Finally, tell the program what to do with these keys...
  29.  
  30.     ON KEY(1) GOSUB F1Key
  31.     ON KEY(13) GOSUB RightArrow
  32.     ON KEY(12) GOSUB LeftArrow
  33.     ON KEY(16) GOSUB AltA
  34.     ON KEY(17) GOSUB AltD
  35.     ON KEY(18) GOSUB AltF
  36.     ON KEY(19) GOSUB AltP
  37.     ON KEY(20) GOSUB Escape
  38.  
  39. 'Here's where your main program would go.  Instead, we'll have some fun!
  40. Start:
  41.     CLS : KEY OFF
  42.     RANDOMIZE (-TIMER)
  43. WaitLoop:
  44.     TIME = RND(500)
  45.     LOCATE 15, 1, 0
  46.     COL = COL + 1
  47.     IF COL > 16 THEN COL = 1
  48.     COLOR COL, 0, 0
  49.     PRINT "I'm waiting for you to press a key..."
  50.     FOR N = 1 TO TIME: NEXT N
  51.     GOTO WaitLoop
  52. END
  53.  
  54. 'Key trapping subroutines are here...
  55.  
  56. F1Key:
  57. ' F1 key pressed - print a message
  58.     LOCATE 20, 1, 0
  59.     PRINT "The last key you pressed was the F1 key...                "
  60.     RETURN
  61.  
  62. RightArrow:
  63. ' Right arrow pressed - print a message
  64.     LOCATE 20, 1, 0
  65.     PRINT "The last key you pressed was the right arrow key...       "
  66.     RETURN
  67.  
  68. LeftArrow:
  69. ' Left arrow pressed - print a message
  70.     LOCATE 20, 1, 0
  71.     PRINT "The last key you pressed was the left arrow key...        "
  72.     RETURN
  73.  
  74. AltA:
  75. ' Alt-A key pressed - print a message
  76.     LOCATE 20, 1, 0
  77.     PRINT "The last key you pressed was the Alt-A key...             "
  78.     RETURN
  79.  
  80. AltD:
  81. ' Alt-D key pressed - print a message
  82.     LOCATE 20, 1, 0
  83.     PRINT "The last key you pressed was the Alt-D key...             "
  84.     RETURN
  85.  
  86. AltF:
  87. ' Alt-F key pressed - print a message
  88.     LOCATE 20, 1, 0
  89.     PRINT "The last key you pressed was the Alt-F key...             "
  90.     RETURN
  91.  
  92. AltP:
  93. ' Alt-P  key pressed - print a message
  94.     LOCATE 20, 1, 0
  95.     PRINT "The last key you pressed was the Alt-P key...             "
  96.     RETURN
  97.  
  98. Escape:
  99. ' Escape key pressed - print a message
  100.     LOCATE 20, 1, 0
  101.     PRINT "The last key you pressed was the ESCAPE key...            "
  102.     RETURN
  103.  
  104.